package gdut.jmxywz.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import gdut.jmxywz.been.Student;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.ThreeDES;

public class FindPasswordAction extends BaseAction {

	private Teacher teacher;
	
	private Student student;
	
	private ThreeDES threeDES;
	
	private String teacherNum;
	
	private String studentNum;
	
	
	public String getTeacherNum() {
		return teacherNum;
	}

	public void setTeacherNum(String teacherNum) {
		this.teacherNum = teacherNum;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public ThreeDES getThreeDES() {
		return threeDES;
	}

	public void setThreeDES(ThreeDES threeDES) {
		this.threeDES = threeDES;
	}
	
	public String getStudentNum() {
		return studentNum;
	}

	public void setStudentNum(String studentNum) {
		this.studentNum = studentNum;
	}
/**
 * 异步实现密码找回
 * @throws Exception
 */
	/*找回教师密码*/
	public void teacherPwd() throws Exception {
		// TODO Auto-generated method stub
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/xml");
	    PrintWriter out = null;
	    /*按工号查询教师密码并解密，teacherPassword为返回的密码或说明*/
	    if(teacherNum!=null){
			 String teacherPassword="";
			 String hql="select t from Teacher t where t.teacherNum='"+teacherNum+"'";
			 List teacherList=this.getCommonService().findListByHql(hql, null);
			 
			 if(teacherList!=null&&teacherList.size()>0){
				 teacher=(Teacher)teacherList.get(0);
				 /*密码解密*/	
				 threeDES.getKey(threeDES.getTeacherKey());
				 teacherPassword=threeDES.getDesString(teacher.getTeacherPassword());
			 }else{
				 teacherPassword="工号不存在";
			 }
			 try {   
		            out = res.getWriter();   
		            out.print("<root>");   
		            out.print("<message>");   
		            out.print(teacherPassword);   
		            out.print("</message>");   
		            out.print("</root>");   
		        } catch (Exception e) {   
		            e.printStackTrace();   
		        } finally {   
		            out.flush();   
		            out.close();   
		        }   
		 }
	}
	
	/*找回学生密码*/
	public void studentPwd() throws Exception {
		// TODO Auto-generated method stub
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/xml");
	    PrintWriter out = null;
		/*按学号查询学生密码并解密，studentPassword为返回的密码或说明*/
		if(studentNum!=null){
			 String studentPassword="";
			 String hql="select stu from Student stu where stu.studentNum='"+studentNum+"'";
			 List studentList=this.getCommonService().findListByHql(hql, null);
			 /*如果新增工号在数据库中存在，则返回字符串true，作为js文件处理标记*/
			 if(studentList!=null&&studentList.size()>0){
				 student=(Student)studentList.get(0);
				 /*密码解密*/	
				 threeDES.getKey(threeDES.getStudentKey());
				 studentPassword=threeDES.getDesString(student.getStudentPwd());
			 }else{
				 studentPassword="该学号不存在...";
			 }
			 try {   
		            out = res.getWriter();   
		            out.print("<root>");   
		            out.print("<message>");   
		            out.print(studentPassword);   
		            out.print("</message>");   
		            out.print("</root>");   
		        } catch (Exception e) {   
		            e.printStackTrace();   
		        } finally {   
		            out.flush();   
		            out.close();   
		        }   
		 }
	}
}
